package com.core.redis.subscriber;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;

import redis.clients.jedis.Jedis;

import com.core.redis.listener.MyListener;
import com.core.redis.util.RedisSubCluster;

public class MySubscriber implements ServletContextListener{

	private static Logger logger = Logger.getLogger(MySubscriber.class);
	
	private MyThread myThread;
	
	public void subcriber(){
		// 获得订阅的jedis connect
		Jedis jedis = RedisSubCluster.getInCluster().getJedis();
		
		jedis.psubscribe(new MyListener(), "test");
		
	}
	
	public static void main(String[] args) {
		MySubscriber sub = new MySubscriber();
	}

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		logger.info("任务执行完毕==================");
		
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		
		String str = null;  
        if (str == null && myThread == null) {  
            myThread = new MyThread();  
            myThread.start(); // servlet 上下文初始化时启动 socket  
        }  
		
	}
}
